package com.ld.intercepter;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @Author: Liudao
 * @Date: 2022-08-17-13:55
 * @Description:
 */
@Component
public class loginIntercepter implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //得到session对象
        HttpSession session = request.getSession();
        //判断session中的username是不是空
        if (StringUtils.isEmpty(session.getAttribute("username"))){
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }
}
